package com.example.tb;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity{

    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case 0:contentButton.setEnabled(false);
                    break;
                case 1:contentButton.setEnabled(true);
                    break;
            }
        }
    };


    private EditText inputBox ;
    private ImageButton contentButton ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inputBox = (EditText) findViewById(R.id.inputBox);
        contentButton = (ImageButton) findViewById(R.id.content);

        inputBox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Thread th = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int flag;

                        if(inputBox.getText().toString().equals("")){
                            flag = 0;
                        }else{
                            flag = 1;
                        }
                        Message msg = new Message();
                        msg.what = flag;
                        handler.sendMessage(msg);
                    }

                });
                th.start();
            }
        });

        contentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(MainActivity.this,ContentActivity.class);
                intent2.putExtra("word",inputBox.getText().toString());
                startActivity(intent2);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.src:
                Intent intent2 = new Intent(MainActivity.this,SrcActivity.class);
                intent2.putExtra("word",inputBox.getText().toString());
                startActivity(intent2);
                break;
            case R.id.web:
                Intent intent = new Intent(MainActivity.this,WebActivity.class);
                intent.putExtra("word",inputBox.getText().toString());
                startActivity(intent);
                break;
            default:
                break;
        }
        return true;
    }
}
